// pages/template/imageShow/index.js
var col1H = 0;
var col2H = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollH: 0,
    imgWidth: 0,
    loadingCount: 0,
    images: [],
    col1: [],
    col2: [],
      imagesUrl:"",
      showLoading: true
  },

  showLoading: function () {
    this.setData({
      showLoading: true
    })
  },
  cancelLoading: function () {
    this.setData({
      showLoading: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    clearTimeout(times)
    var that = this;
    that.showLoading();
    var times =  setTimeout(function(){
      that.cancelLoading();
    },3000)
      var value = wx.getStorageSync('imagesUrl')
      that.setData({
          imagesUrl:value
      })

    wx.getSystemInfo({
      success: function (res) {


        var ww = res.windowWidth;
        var wh = res.windowHeight;
        var imgWidth = ww * 0.48;
        var scrollH = wh;

        that.setData({
          scrollH: scrollH,
          imgWidth: imgWidth
        });

        //加载首组图片
        that.loadImages();
      }
    })
  },
    onImageLoad:function (e) {
        var that = this;
        var imageId = e.currentTarget.id;
        var oImgW = e.detail.width;         //图片原始宽度
        var oImgH = e.detail.height;        //图片原始高度
        var imgWidth = that.data.imgWidth;  //图片设置的宽度
        var scale = imgWidth / oImgW;        //比例计算
        var imgHeight = oImgH * scale;      //自适应高度

        var images = that.data.images;
        var imageObj = null;
        for (var i = 0; i < images.length; i++) {
            var img = images[i];
            if (img.id === imageId) {
                imageObj = img;
                break;
            }
        }
        imageObj.height = imgHeight;

        var loadingCount = that.data.loadingCount - 1;
        var col1 = that.data.col1;
        var col2 = that.data.col2;
        //判断当前图片添加到左列还是右列
        if (col1H <= col2H) {
            col1H += imgHeight;
            col1.push(imageObj);
        } else {
            col2H += imgHeight;
            col2.push(imageObj);
        }
        var data = {
            loadingCount: loadingCount,
            col1: col1,
            col2: col2
        };
        //当前这组图片已加载完毕，则清空图片临时加载区域的内容
        if (!loadingCount) {
            data.images = [];
        }
        that.setData(data);

    },
    loadImages:function () {
    var that = this;
       var  images= that.data.imagesUrl;
        // console.log("===");
        // console.log(images);
        // console.log("===");
        var baseId = "img-" + (+new Date());

        for (var i = 0; i < images.length; i++) {
            images[i].id = baseId + "-" + i;
        }
        that.setData({
            loadingCount: images.length,
            images: images
        });
    },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // this.cancelLoading();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
 
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})